import os

import numpy as np
import cv2 as cv


if __name__ == "__main__1":
    # folder = "E:/datasets/QXSLAB_SAROPT/opt_256_oc_0.2"
    # save_folder = "E:/datasets/client-data/QXS-merge/opt"
    folder = "E:/datasets/QXSLAB_SAROPT/sar_256_oc_0.2"
    save_folder = "E:/datasets/client-data/QXS-merge/sar"

    range_max = 20000
    last = None
    name_list = []
    for i in range(1, range_max+1):
        file_name = "/".join((folder, f"{i}.png"))
        cur_img = cv.imread(filename=file_name)

        if not last is None:
            start_col = -1
            
            for c in range(256):
                col = cur_img[:, c]
                if np.abs(col-last[:, -1]).sum() < 10:
                    start_col = c
                    break

            if start_col == -1:
                cv.imwrite("/".join((save_folder, f"{name_list[0]}_{name_list[-1]}.png")), last)
                name_list = []
                last = None
            else:
                name_list.append(i)

        if last is None:
            last = cur_img
            name_list.append(i)
        else:
            last = np.concatenate([last, cur_img[:, start_col+1:]], axis=1)

        # if i == range_max:
        #     cv.imwrite("/".join((save_folder, f"{name_list[0]}_{name_list[-1]}.png")), last)

        # print(name_list)
        # print(i)


if __name__ == "__main__1":

    def merge_band(img1, img2, c1, c2, row=50):

        h1, w1 = img1.shape[0], img1.shape[1]
        h2, w2 = img2.shape[0], img2.shape[1]

        # print(h1, w1, h2, w2, c1, c2)

        left_len1, left_len2 = c1[0], c2[0]
        right_len1, right_len2 = w1-c1[1], w2-c2[1]

        left_dis = min(left_len1, left_len2)
        right_dis = min(right_len1, right_len2)
        
        begin1, end1 = c1[0]-left_dis, c1[1]+right_dis
        begin2, end2 = c2[0]-left_dis, c2[1]+right_dis

        img1 = img1[:, begin1:end1]
        img2 = img2[row+1:, begin2:end2]
        # print(img.shape, img2.shape)

        img = np.concatenate([img1,img2], axis=0)
        return img

    patch_list = [
        "E:/datasets/client-data/QXS-merge/sar-band/4353_4370.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4371_4394.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4395_4424.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4425_4460.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4461_4501.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4502_4542.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4543_4583.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4584_4624.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4625_4664.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4665_4704.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4705_4745.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4746_4786.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4787_4827.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4828_4868.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4869_4909.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4910_4950.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4951_4991.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/4992_5032.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5033_5073.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5074_5114.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5115_5155.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5156_5196.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5197_5237.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5238_5278.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5279_5319.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5320_5361.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5362_5402.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5403_5443.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5444_5484.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5485_5525.png", 
        "E:/datasets/client-data/QXS-merge/sar-band/5526_5563.png", 
    ]
    save_folder = "E:/datasets/client-data/QXS-merge/sar-band/res"

    patch1 = patch_list[0]
    for i in range(1, len(patch_list)):

        patch2 = patch_list[i]
        fname = save_folder + "/"  + "merge_to_" + os.path.basename(patch2)

        img1 = cv.imread(patch1)
        img2 = cv.imread(patch2)

        print(patch1)
        h, w = img1.shape[0], img1.shape[1]
        c_begin, c_end = w//2-128, w//2+128
        h1, w1 = img2.shape[0], img2.shape[1]

        row = 50
        flag = False
        for i in range(0, w1-256):

            if np.abs(img1[-1, c_begin:c_end]-img2[row, i:i+256]).sum() < 10:
                img = merge_band(
                    img1, img2, 
                    (c_begin, c_end), (i, i+256), row=row
                )    
                cv.imwrite(fname, img)
                flag = True
                break

        if flag == False: 
            row = 51
            for i in range(0, w1-256):

                if np.abs(img1[-1, c_begin:c_end]-img2[row, i:i+256]).sum() < 10:
                    img = merge_band(
                        img1, img2, 
                        (c_begin, c_end), (i, i+256), row=row
                    )    
                    cv.imwrite(fname, img)
                    flag = True
                    break

        if flag == False:
            for row in range(0, h1):

                for i in range(0, w1-256):

                    if np.abs(img1[-1, c_begin:c_end]-img2[row, i:i+256]).sum() < 10:
                        img = merge_band(
                            img1, img2, 
                            (c_begin, c_end), (i, i+256), row=row
                        )    
                        cv.imwrite(fname, img)
                        flag = True
                        break


        patch1 = fname


if __name__ == "__main__":
    opt_total = "E:/datasets/client-data/QXS-merge/opt-band/res/merge_to_5526_5563.png"
    path = "C:/Users/FU/Desktop/show-detail1024.png"
    save_path = "C:/Users/FU/Desktop/test.png"
    total_save_path = "C:/Users/FU/Desktop/total.png"

    img = cv.imread(path)

    img[256:768:2, 256, 0] = 0
    img[256:768:2, 256, 1] = 255
    img[256:768:2, 256, 2] = 255
    img[256:768:2, 767, 0] = 0
    img[256:768:2, 767, 1] = 255
    img[256:768:2, 767, 2] = 255
    img[256, 256:768:2, 0] = 0
    img[256, 256:768:2, 1] = 255
    img[256, 256:768:2, 2] = 255
    img[767, 256:768:2, 0] = 0
    img[767, 256:768:2, 1] = 255
    img[767, 256:768:2, 2] = 255
    
    img[384:640:2, 384, 0] = 0
    img[384:640:2, 384, 1] = 0
    img[384:640:2, 384, 2] = 255
    img[384:640:2, 639, 0] = 0
    img[384:640:2, 639, 1] = 0
    img[384:640:2, 639, 2] = 255
    img[384, 384:640:2, 0] = 0
    img[384, 384:640:2, 1] = 0
    img[384, 384:640:2, 2] = 255
    img[639, 384:640:2, 0] = 0
    img[639, 384:640:2, 1] = 0
    img[639, 384:640:2, 2] = 255
    
    img[386:642:2, 390, 0] = 0
    img[386:642:2, 390, 1] = 255
    img[386:642:2, 390, 2] = 0
    img[386:642:2, 645, 0] = 0
    img[386:642:2, 645, 1] = 255
    img[386:642:2, 645, 2] = 0
    img[386, 390:646:2, 0] = 0
    img[386, 390:646:2, 1] = 255
    img[386, 390:646:2, 2] = 0
    img[641, 390:646:2, 0] = 0
    img[641, 390:646:2, 1] = 255
    img[641, 390:646:2, 2] = 0

    img = img[216:808, 216:808]

    cv.imwrite(save_path, img=img)
    
    img = cv.imread(opt_total)
    # img = img[1848:2440, 2712:3304, 0] = 255
    # img = img[1848:2440, 2712:3304, 1] = 255
    # img = img[1848:2440, 2712:3304, 2] = 255
    img[1848:2440, 2700:2713, 0] = 255
    img[1848:2440, 2700:2713, 1] = 255
    img[1848:2440, 2700:2713, 2] = 255
    img[1848:2440, 3303:3316, 0] = 255
    img[1848:2440, 3303:3316, 1] = 255
    img[1848:2440, 3303:3316, 2] = 255
    img[1836:1849, 2712:3304, 0] = 255
    img[1836:1849, 2712:3304, 1] = 255
    img[1836:1849, 2712:3304, 2] = 255
    img[2439:2452, 2712:3304, 0] = 255
    img[2439:2452, 2712:3304, 1] = 255
    img[2439:2452, 2712:3304, 2] = 255
    cv.imwrite(total_save_path, img=img)
